
#include <assert.h>
#include <colorpicker/colorpicker.h>
#include <material/materialeditor/materialeditordialog.h>
#include <config.h>
#include <session.h>
#include <qfileinfo.h>
#include <qgraphicssceneevent.h>
#include <qmessagebox.h>
#include <qmime.h>



/* ***********************************************
 *   cceTextureSlot
 * ***********************************************/



/* ***********************************************
 *   cceMaterialBoolValueItem
 * ***********************************************/

cceMaterialBoolValueItem::cceMaterialBoolValueItem (const QString& name)
  : cceMaterialSimpleValueItem ()
  , _active (false)
  , _name (name)
  , _checked (false)
{
  SetName (name + " : Off");
}

void cceMaterialBoolValueItem::SetChecked(bool checked)
{
  _checked = checked;
  SetName (_name + " : " + (checked ? "On" : "Off"));
}


void cceMaterialBoolValueItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  grabMouse();
  _active = true;
}

void cceMaterialBoolValueItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  bool a = _active;
  _active = false;
  ungrabMouse();
  if (a && _background->rect().contains(event->pos()))
    {
      SetChecked(!_checked);
			GetView()->UpdateLayout();
			emit Changed ();
    }
}

/* ***********************************************
 *   cceMaterialColorValueItem
 * ***********************************************/

cceMaterialColorValueItem::cceMaterialColorValueItem (const QString &name)
  : cceMaterialSimpleValueItem ()
  , _active (false)
{
  QString n= QString ("<<%1 - Color>>").arg (name);
  SetName (n);
}

void cceMaterialColorValueItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  grabMouse();
  _active = true;
}

void cceMaterialColorValueItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  bool a = _active;
  _active = false;
  ungrabMouse();
  if (a && _background->rect().contains(event->pos()))
    {
      QColor color = cceColorPickerDialog::GetColor(GetView(), _color, "Select color");

      bool changed = color.rgb() != _color.rgb();
      SetColor(color);

      if (changed)
				{
					emit Changed();
				}
    }
}


void cceMaterialColorValueItem::SetColor(const QColor &color)
{
  _color = color;
  SetBackgroundColor(color);
  if (color.valueF() < 0.5f)
    {
      SetTextColor(QColor (255, 255, 255));
    }
  else
    {
      SetTextColor(QColor (0, 0, 0));
    }

}


/* ***********************************************
 *   cceMaterialShininessValueItem
 * ***********************************************/

cceMaterialShininessValueItem::cceMaterialShininessValueItem ()
  : cceMaterialSimpleValueItem ()
{
  SetShininess(16.0f);
}


void cceMaterialShininessValueItem::SetShininess(float shininess)
{
  _shininess = shininess;

  QString name = QString ("<<Shininess : %1>>").arg(shininess);
  SetName(name);

}



/* ***********************************************
 *   cceMaterialSpecTechniqueItem
 * ***********************************************/

cceMaterialMaterialSpecTechniqueItem::cceMaterialMaterialSpecTechniqueItem ()
  : cceMaterialTechniqueItem (false, false, false)
{
  SetName("<<Spec>>");

  _colorAmbient = new cceMaterialColorValueItem ("Ambient");
  _colorDiffuse = new cceMaterialColorValueItem ("Diffuse");
  _colorSpecular = new cceMaterialColorValueItem ("Specular");
  _shininess = new cceMaterialShininessValueItem ();
  _colorEmission = new cceMaterialColorValueItem ("Emission");
  _writeDepth = new cceMaterialBoolValueItem ("Write Depth");
  _testDepth = new cceMaterialBoolValueItem ("Test Depth");
  _transparent = new cceMaterialBoolValueItem ("Transparent");
  AddChild(_colorAmbient);
  AddChild(_colorDiffuse);
  AddChild(_colorSpecular);
  AddChild(_shininess);
  AddChild(_colorEmission);
  AddChild(_writeDepth);
  AddChild(_testDepth);
  AddChild(_transparent);

	connect (_colorAmbient, SIGNAL(Changed()), this, SIGNAL(Changed()));
	connect (_colorDiffuse, SIGNAL(Changed()), this, SIGNAL(Changed()));
	connect (_colorSpecular, SIGNAL(Changed()), this, SIGNAL(Changed()));
	connect (_shininess, SIGNAL(Changed()), this, SIGNAL(Changed()));
	connect (_colorEmission, SIGNAL(Changed()), this, SIGNAL(Changed()));
	connect (_writeDepth, SIGNAL(Changed()), this, SIGNAL(Changed()));
	connect (_testDepth, SIGNAL(Changed()), this, SIGNAL(Changed()));
	connect (_transparent, SIGNAL(Changed()), this, SIGNAL(Changed()));
}


/* ***********************************************
 *   cceMaterialEffectSlotItem
 * ***********************************************/

cceMaterialEffectSlotItem::cceMaterialEffectSlotItem (cceMaterialMaterialPrimaryItem  *primary)
  : cceMaterialSlotItem ()
  , _primary (primary)
{
  SetName ("<<Attach Effect>>");
}

namespace
{
cceMaterialEffect *read_effect_from_mime (const QMimeData* mimeData)
{
  if (!mimeData)
    {
      return 0;
    }

  if (!mimeData->hasFormat (CCE_MATERIAL_EFFECTNAME_MIME))
    {
      return 0;
    }

  QByteArray ba = mimeData->data (CCE_MATERIAL_EFFECTNAME_MIME);
  return cceSession::Get()->GetMaterialManager()->GetEffect(QString(ba));
}
}



void cceMaterialEffectSlotItem::dragEnterEvent(QGraphicsSceneDragDropEvent *evt)
{
  cceMaterialEffect* effect = read_effect_from_mime (evt->mimeData ());
  if (effect)
    {
      evt->accept ();
    }
  else
    {
      evt->ignore ();
    }

}

void cceMaterialEffectSlotItem::dropEvent(QGraphicsSceneDragDropEvent *evt)
{
  cceMaterialEffect* effect = read_effect_from_mime (evt->mimeData ());
  if (!effect)
    {
      return;
    }

	_primary->ChangeEffect(effect);
  _primary->GetView()->UpdateLayout();

}



/* ***********************************************
 *   cceMaterialEffectValueItem
 * ***********************************************/


cceMaterialEffectValueItem::cceMaterialEffectValueItem (cceMaterialMaterialPrimaryItem  *primary)
  : cceMaterialValueItem ()
  , _primary (primary)
{
  setAcceptDrops (true);

  SetType("<<Effect>>");

  connect (this, SIGNAL(Clicked()), this, SLOT(onShowEffect()));
}

void cceMaterialEffectValueItem::onShowEffect()
{
  emit ShowEditor (_effect);
}

void cceMaterialEffectValueItem::SetEffect(cceMaterialEffect *effect)
{
  _effect = effect;
  connect (_effect, SIGNAL(Removed()), this, SLOT (RemoveValue()));
  connect (_effect, SIGNAL(Changed()), this, SLOT(onEffectChanged()));
  SetName(effect->GetName());
}

void cceMaterialEffectValueItem::onEffectChanged()
{
	_primary->ChangeEffect(_effect);
}

void cceMaterialEffectValueItem::RemoveValue()
{
	_primary->ChangeEffect(0);
  _primary->GetView()->UpdateLayout();
}



void cceMaterialEffectValueItem::dragEnterEvent(QGraphicsSceneDragDropEvent *evt)
{
  cceMaterialEffect* effect = read_effect_from_mime (evt->mimeData ());
  if (effect)
    {
      evt->accept ();
    }
  else
    {
      evt->ignore ();
    }

}

void cceMaterialEffectValueItem::dropEvent(QGraphicsSceneDragDropEvent *evt)
{
  cceMaterialEffect* effect = read_effect_from_mime (evt->mimeData ());
  if (!effect)
    {
      return;
    }

	_primary->ChangeEffect(effect);
  _primary->GetView()->UpdateLayout();
}


/* ***********************************************
 *   cceMaterialTextureTechniqueItem
 * ***********************************************/


cceMaterialTextureTechniqueItem::cceMaterialTextureTechniqueItem (cceMaterialPass pass)
  : cceMaterialTechniqueItem (false, false, false)
{
  _item = new cceMaterialTexturesItem (pass);
	connect (_item, SIGNAL(Changed()), this, SIGNAL(Changed()));
	connect (_item, SIGNAL(ShowEditor(cceMaterialItem*)), this, SIGNAL(ShowEditor(cceMaterialItem*)));

  QString name = QString ("<<%1 - Textures>>").arg (cceMaterialEffectStage::GetPassName(pass));
  SetName(name);

}


void cceMaterialTextureTechniqueItem::Clear()
{
  _item->Clear();
}

void cceMaterialTextureTechniqueItem::ClearActives()
{
  _item->ClearActives();
}

void cceMaterialTextureTechniqueItem::SetTextureNames(unsigned unit, const QSet<QString> &names)
{
  _item->SetTextureNames(unit, names);
}



void cceMaterialTextureTechniqueItem::SetTexture(unsigned unit, cceMaterialTexture *texture)
{
  _item->SetTexture(unit, texture);

  Clear();
  UpdateItems();
}

void cceMaterialTextureTechniqueItem::UpdateItems()
{
  bool found = false;

  for (unsigned i=0; i<16; ++i)
    {
      if (_item->IsActive(i))
        {
          found = true;
          break;
        }
    }

  if (found && !Contains(_item))
    {
      AddChild(_item);
    }
  else if (!found && Contains (_item))
    {
      RemoveChild (_item);
    }

  _item->UpdateSize ();
}

cceMaterialTexture *cceMaterialTextureTechniqueItem::GetTexture(unsigned unit)
{
  return _item->GetTexture (unit);
}



/* ***********************************************
 *   cceMaterialTextureItem
 * ***********************************************/


cceMaterialTextureItem::cceMaterialTextureItem (cceMaterialTexturesItem *parent, unsigned unit)
  : QGraphicsRectItem (parent)
  , QObject ()
  , _active (false)
  , _unit (unit)
  , _texture(0)
  , _parent (parent)
	, _hot(false)
{
	setAcceptHoverEvents (true);
	setAcceptedMouseButtons (Qt::LeftButton);
	setAcceptDrops(true);
  QFont font;
  font.setPixelSize(11);
  font.setBold(false);
  font.setFamily("Helvetica");

  QPen textPen = QPen(QColor(0, 0, 0), 0.1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);

  _name = new QGraphicsSimpleTextItem (this);
  _name->setFont(font);
  _name->setPen(textPen);

  _type = new QGraphicsSimpleTextItem (this);
  _type->setFont(font);
  _type->setPen(textPen);

  _noTexture = new QGraphicsRectItem (this);
  _noTexture->setBrush(QColor (255, 255, 255));
  _noTexture->setPen(QColor (0, 0, 0));
  _noTexture->setRect(0, 0, 128, 128);
  _noTexture->setPos(CCE_MATERIAL_ITEM_HBORDER, CCE_MATERIAL_ITEM_HBORDER);
  _noTexture->setVisible(true);



  QGraphicsLineItem* lineItem;
  lineItem = new QGraphicsLineItem (_noTexture);
  lineItem->setPen(QColor (0, 0, 0));
  lineItem->setLine(0, 0, 128, 128);

  lineItem = new QGraphicsLineItem (_noTexture);
  lineItem->setPen(QColor (0, 0, 0));
  lineItem->setLine(0, 128, 128, 0);

  _noTexture->setVisible(false);

  _textureItem = new QGraphicsPixmapItem (this);
  _textureItem->setPos(CCE_MATERIAL_ITEM_HBORDER, CCE_MATERIAL_ITEM_HBORDER);
  _textureItem->setVisible(false);

  _remove = new cceMaterialRemoveItem (_textureItem);
  _remove->setPos(128.0f - CCE_MATERIAL_BUTTON_SIZE - 3, 2);
  _remove->setVisible(true);

  connect (_remove, SIGNAL(clicked()), this, SLOT(remove()));

  QFontMetrics fm (font);
  int height = fm.height();

  setBrush(QColor (255, 255, 255));
  setPen(QColor (0, 0, 0));

  setRect(0.0f, 0.0f, 128.0f + 2.0f * CCE_MATERIAL_ITEM_HBORDER , 128.0f + 2.0f * height + 4.0f * CCE_MATERIAL_ITEM_HBORDER);


  UpdateItems();
}

cceMaterialView* cceMaterialTextureItem::GetView()
{
  return _parent->GetView();
}

void cceMaterialTextureItem::remove()
{
	ChangeTexture(0);
  GetView()->UpdateLayout();
}

void cceMaterialTextureItem::SetType(const QString &type)
{
  _type->setText(type);
  UpdateItems();
}

void cceMaterialTextureItem::SetActive(bool active)
{
  _active = active;

  setVisible(active);
}

void cceMaterialTextureItem::UpdateItems()
{
  unsigned width = rect().width();
  unsigned height = rect().height();

  QFontMetrics fm (_name->font());
  int nameWidth = fm.width(_name->text());
  int typeWidth = fm.width(_type->text());

  _name->setPos((width - nameWidth) / 2, height - 1.0f * CCE_MATERIAL_ITEM_HBORDER - 1.0f * fm.height());
  _type->setPos((width - typeWidth) / 2, height - 2.0f * CCE_MATERIAL_ITEM_HBORDER - 2.0f * fm.height());

  _noTexture->setVisible(_texture == 0);
  _textureItem->setVisible(_texture != 0);
}

void cceMaterialTextureItem::TextureChanged()
{
	SetTexture(_texture);
	GetView()->UpdateLayout();
}

void cceMaterialTextureItem::TextureRemoved()
{
	ChangeTexture(0);
	GetView()->UpdateLayout();
}

void cceMaterialTextureItem::ChangeTexture(cceMaterialTexture *texture)
{
	SetTexture(texture);
	emit Changed ();
}

void cceMaterialTextureItem::SetTexture(cceMaterialTexture *texture)
{
	if (_texture)
		{
			disconnect (_texture, SIGNAL(Changed()), this, SLOT(TextureChanged ()));
			disconnect (_texture, SIGNAL(Removed()), this, SLOT(TextureRemoved ()));
		}
  _texture = texture;
  if (_texture)
    {
			connect (_texture, SIGNAL(Changed()), this, SLOT(TextureChanged ()));
			connect (_texture, SIGNAL(Removed()), this, SLOT(TextureRemoved ()));

      _name->setText(_texture->GetName());

			QString filename = "/" CCE_MATERIAL_TEXTURES_FOLDER "/"+_texture->GetFilename();
			filename = cceSession::Get()->GetFileName(filename);
			QImage img (filename);
			if (img.load(filename))
				{
					img = img.scaled(128, 128,  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
					QPixmap pm = QPixmap::fromImage(img);
					_textureItem->setPixmap(pm);
				}
			else
				{
					_textureItem->setPixmap(QPixmap());
				}
    }
  else
    {
      _name->setText("");
    }

  UpdateItems();
}


void cceMaterialTextureItem::dragEnterEvent (QGraphicsSceneDragDropEvent* evt)
{
  const QMimeData* md = evt->mimeData();
  if (md->hasFormat (CCE_FILESYSTEM_MIME))
    {
      QString name = md->data(CCE_FILESYSTEM_MIME);

      QFileInfo info (name);
      QString ext = info.suffix().toLower().trimmed();
      if (ext == "jpg" || ext == "jpeg" ||
          ext == "png" || ext == "tga" ||
          ext == "bmp")
        {
          evt->accept();
          return;
        }
    }
  else if (md->hasFormat(CCE_MATERIAL_TEXTURENAME_MIME))
    {
      evt->accept();
      return;
    }

  evt->ignore();
}

void cceMaterialTextureItem::dropEvent (QGraphicsSceneDragDropEvent* evt)
{
  const QMimeData* md = evt->mimeData();

  if (md->hasFormat (CCE_FILESYSTEM_MIME))
    {
      QString name = md->data(CCE_FILESYSTEM_MIME);
      QFileInfo info (name);
      QString ext = info.suffix().toLower().trimmed();
      if (ext == "jpg" || ext == "jpeg" ||
          ext == "png" || ext == "tga" ||
          ext == "bmp")
        {
          cceMaterialTexture* texture = cceSession::Get()->GetMaterialManager()->GetTextureByFilename(name, true);
					ChangeTexture(texture);
          GetView()->UpdateLayout();
          return;
        }
    }
  else if (md->hasFormat(CCE_MATERIAL_TEXTURENAME_MIME))
    {
      QString name = md->data(CCE_MATERIAL_TEXTURENAME_MIME);
      cceMaterialTexture* texture = cceSession::Get()->GetMaterialManager()->GetTexture(name);
			ChangeTexture (texture);
      GetView()->UpdateLayout();
			return;
    }

  evt->ignore();
}

void cceMaterialTextureItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	if (!_texture)
		{
			return;
		}

	grabMouse();
	_hot = true;
}

void cceMaterialTextureItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	if (!_texture)
		{
			return;
		}
	ungrabMouse();
	if (!boundingRect().contains(event->pos()))
		{
			return;
		}

	if (!_hot)
		{
			return;
		}

	_hot = false;


	emit ShowEditor (_texture);
}

void cceMaterialTextureItem::hoverEnterEvent(QGraphicsSceneHoverEvent *evt)
{
	if (!_texture)
		{
			setCursor(Qt::ArrowCursor);
		}
	else
		{
			setCursor(Qt::PointingHandCursor);
		}
}

void cceMaterialTextureItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *evt)
{
	setCursor(Qt::ArrowCursor);
}

/* ***********************************************
 *   cceMaterialTexturesItem
 * ***********************************************/


cceMaterialTexturesItem::cceMaterialTexturesItem (cceMaterialPass pass)
  : cceMaterialBaseItem ()
{
  SetUnifiedSize(false);
  _background = new QGraphicsRectItem (this);
  _background->setBrush(QColor(255, 255, 255));
  _background->setPen(QColor(0, 0, 0));

  for (unsigned i=0; i<16; ++i)
    {
      _textureItems[i] = new cceMaterialTextureItem (this, i);
			connect (_textureItems[i], SIGNAL(Changed()), this, SIGNAL(Changed()));
			connect (_textureItems[i], SIGNAL(ShowEditor(cceMaterialItem*)), this, SIGNAL(ShowEditor(cceMaterialItem*)));
    }
}


void cceMaterialTexturesItem::Clear()
{

}

void cceMaterialTexturesItem::ClearActives()
{
  for (unsigned i=0; i<16; ++i)
    {
      _textureItems[i]->SetActive(false);
    }
}

void cceMaterialTexturesItem::SetTextureNames (unsigned unit, const QSet<QString> &names)
{
  QStringList list = names.toList();
  _textureItems[unit]->SetType(list.join(", "));
  _textureItems[unit]->SetActive(true);
}

void cceMaterialTexturesItem::SetTexture(unsigned unit, cceMaterialTexture *texture)
{
  _textureItems[unit]->SetTexture(texture);
}

cceMaterialTexture *cceMaterialTexturesItem::GetTexture(unsigned unit)
{
  return _textureItems[unit]->GetTexture();
}

void cceMaterialTexturesItem::ComputeSize()
{
  unsigned width = 0;
  unsigned height = 0;
  for (unsigned i=0; i<16; ++i)
    {
      if (_textureItems[i]->IsActive())
        {
          width += _textureItems[i]->rect().width();
          height = height > _textureItems[i]->rect().height() ? height : _textureItems[i]->rect().height();
        }
    }

  _width = width;
  _height = height;
}

void cceMaterialTexturesItem::UpdateLayout()
{
  _background->setRect(0, 0, _width, _height);

  unsigned pos = 0;
  for (unsigned i=0; i<16; ++i)
    {
      if (_textureItems[i]->IsActive())
        {
          _textureItems[i]->setPos(pos, 0);
          pos += _textureItems[i]->rect().width();
        }
    }
}

void cceMaterialTexturesItem::UpdateSize()
{
  ComputeSize();
  UpdateLayout();
}


/* ***********************************************
 *   cceMaterialMaterialPrimaryItem
 * ***********************************************/


cceMaterialMaterialPrimaryItem::cceMaterialMaterialPrimaryItem ()
  : cceMaterialPrimaryItem (0, false)
  , _effect(0)
  , _effectSlot(0)
  , _effectValue(0)
{
  SetType("<<Material>>");

  _specItem = new cceMaterialMaterialSpecTechniqueItem ();
	connect (_specItem, SIGNAL(Changed()), this, SIGNAL(Changed()));
  AddChild(_specItem);


  _effectSlot = new cceMaterialEffectSlotItem (this);
  _effectValue = new cceMaterialEffectValueItem (this);
  AddChild(_effectSlot);

  connect (_effectValue, SIGNAL(ShowEditor(cceMaterialItem*)), this, SIGNAL(ShowEditor(cceMaterialItem*)));

  for (unsigned i=0; i<Pass_COUNT; ++i)
    {
      _texturesItems[i] = new cceMaterialTextureTechniqueItem ((cceMaterialPass)i);
			connect (_texturesItems[i], SIGNAL(Changed()), this, SIGNAL(Changed()));
			connect (_texturesItems[i], SIGNAL(ShowEditor(cceMaterialItem*)), this, SIGNAL(ShowEditor(cceMaterialItem*)));
      AddChild(_texturesItems[i]);
    }

}

void cceMaterialMaterialPrimaryItem::ChangeEffect(cceMaterialEffect *effect)
{
	SetEffect(effect);
	emit Changed();
}

void cceMaterialMaterialPrimaryItem::SetEffect(cceMaterialEffect *effect)
{
  if (effect != _effect)
    {
      if (_effect)
        {
          RemoveChild(_effectValue);
        }
      else
        {
          RemoveChild(_effectSlot);
        }
      _effect = effect;

      if (_effect)
        {
          _effectValue->SetEffect (effect);
          AddChild(_effectValue, 1);
        }
      else
        {
          AddChild(_effectSlot, 1);
        }
    }

  for (unsigned pass=0; pass<Pass_COUNT; ++pass)
    {
      _texturesItems[pass]->Clear ();
      _texturesItems[pass]->ClearActives();
    }

  if (_effect)
    {
      QList<cceEffectTextureBindings> bindings = _effect->GetTextureRequirements();
      foreach (const cceEffectTextureBindings& bnd, bindings)
        {
          _texturesItems[bnd.Pass]->SetTextureNames (bnd.Unit, bnd.Bindings);
        }
    }

  for (unsigned pass=0; pass<Pass_COUNT; ++pass)
    {
      _texturesItems[pass]->UpdateItems();
    }
}

cceMaterialEffect* cceMaterialMaterialPrimaryItem::GetEffect()
{
  return _effect;
}


void cceMaterialMaterialPrimaryItem::SetTexture(cceMaterialPass pass, unsigned unit, cceMaterialTexture *texture)
{
  _texturesItems[pass]->SetTexture(unit, texture);
}

cceMaterialTexture *cceMaterialMaterialPrimaryItem::GetTexture(cceMaterialPass pass, unsigned unit)
{
  return _texturesItems[pass]->GetTexture(unit);
}


/* ***********************************************
 *   cceMaterialMaterialEditorDialog
 * ***********************************************/


cceMaterialMaterialEditorDialog::cceMaterialMaterialEditorDialog(QWidget* parent)
  : cceMaterialEditorAbstract (parent)
//	, _primaryItem (0)
{
  _gui.setupUi(this);

//  _materialEditorView = new cceMaterialView (_gui.frameEditor);
//  _materialEditorView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
//  _materialEditorView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

//  QGridLayout* contentLayout = new QGridLayout (_gui.frameEditor);
//  contentLayout->setContentsMargins (0, 0, 0, 0);
//  contentLayout->addWidget (_materialEditorView, 0, 0, 1, 1);

}

void cceMaterialMaterialEditorDialog::SetMaterial(cceMaterialMaterial *material)
{
  _material = material;
//	connect (_material, SIGNAL(Removed()), this, SLOT(ForceClose()));

//  UpdateGUIFromMaterial ();
}

//void cceMaterialMaterialEditorDialog::ForceClose()
//{
//	emit CloseEditor(_material);
//}

QString cceMaterialMaterialEditorDialog::GetName() const
{
	if (_material)
		{
			return _material->GetName();
		}
	return "";
}

bool cceMaterialMaterialEditorDialog::Save()
{
  return true;
//  return UpdateMaterialFromGUI();
}


//void cceMaterialMaterialEditorDialog::on_lineEditName_textChanged(const QString &name)
//{
//	if (_primaryItem)
//		{
//			_primaryItem->SetName(name + ".mat");
//			_materialEditorView->UpdateLayout();
//		}
//  SetChanged(MaterialChanged());
//}

//void cceMaterialMaterialEditorDialog::Close()
//{
//  if (MaterialChanged ())
//    {
//			int result = QMessageBox::question (this,
//				"Material changed - " CCE_TOOL_NAME,
//				"The material has unsaved changes. Save changes bevor closing?",
//				QMessageBox::Save, QMessageBox::Close, QMessageBox::Cancel);
//			switch (result)
//				{
//				case QMessageBox::Save:
//					Save ();
//					break;

//				case QMessageBox::Cancel:
//					return;

//				case QMessageBox::Close:
//					break;
//				}

//    }
//  emit CloseEditor(_material);
//}


//bool cceMaterialMaterialEditorDialog::MaterialChanged()
//{
//  if (!_material || !_primaryItem)
//    {
//      return true;
//    }
//	if (_gui.lineEditName->text() != GetEditorName(_material->GetName())) return true;

//  cceMaterialMaterialSpecTechniqueItem* spec = _primaryItem->GetSpec();
//  if (spec->GetAmbient()->GetColor() != _material->GetAmbient()) return true;
//  if (spec->GetDiffuse()->GetColor() != _material->GetDiffuse()) return true;
//  if (spec->GetSpecular()->GetColor() != _material->GetSpecular()) return true;
//  if (spec->GetShininess()->GetShininess() != _material->GetShininess()) return true;
//  if (spec->GetEmission()->GetColor() != _material->GetEmission()) return true;
//  if (spec->GetTestDepth()->IsChecked() != _material->IsTestDepth()) return true;
//  if (spec->GetWriteDepth()->IsChecked() != _material->IsWriteDepth()) return true;
//  if (spec->GetTransparent()->IsChecked() != _material->IsTransparent()) return true;

//  if (_primaryItem->GetEffect() != _material->GetEffect()) return true;

//  for (unsigned p=0; p<Pass_COUNT; ++p)
//    {
//      cceMaterialPass pass = (cceMaterialPass)p;
//      for (unsigned u=0; u<16; ++u)
//        {
//          if (_primaryItem->GetTexture(pass, u) != _material->GetTexture(pass, u)) return true;
//        }
//    }

//  return false;
//}

//void cceMaterialMaterialEditorDialog::Changed ()
//{
//  SetChanged(MaterialChanged());
//}

//void cceMaterialMaterialEditorDialog::UpdateGUIFromMaterial()
//{
//  assert (_material);
//	_gui.lineEditName->setText(GetEditorName(_material->GetName()));
//  _primaryItem = new cceMaterialMaterialPrimaryItem ();
//  _primaryItem->SetName (_material->GetName ());

//	connect (_primaryItem, SIGNAL(Changed()), this, SLOT(Changed ()));

//  cceMaterialMaterialSpecTechniqueItem* spec = _primaryItem->GetSpec();
//  spec->GetAmbient()->SetColor(_material->GetAmbient());
//  spec->GetDiffuse()->SetColor(_material->GetDiffuse());
//  spec->GetSpecular()->SetColor(_material->GetSpecular());
//  spec->GetShininess()->SetShininess(_material->GetShininess());
//  spec->GetEmission()->SetColor(_material->GetEmission());
//  spec->GetTestDepth()->SetChecked(_material->IsTestDepth());
//  spec->GetWriteDepth()->SetChecked(_material->IsWriteDepth());
//  spec->GetTransparent()->SetChecked(_material->IsTransparent());

//  _primaryItem->SetEffect(_material->GetEffect());

//  for (unsigned p=0; p<Pass_COUNT; ++p)
//    {
//      cceMaterialPass pass = (cceMaterialPass)p;
//      for (unsigned u=0; u<16; ++u)
//        {
//          _primaryItem->SetTexture(pass, u, _material->GetTexture(pass, u));
//        }
//    }



//  connect (_primaryItem, SIGNAL(ShowEditor(cceMaterialItem*)), this, SIGNAL(ShowEditor(cceMaterialItem*)));

//  _materialEditorView->SetItem (_primaryItem);
//}


//bool cceMaterialMaterialEditorDialog::UpdateMaterialFromGUI()
//{
//	assert (_material);

//	cceMaterialMaterial *mat = _manager->GetMaterial(_gui.lineEditName->text() + ".mat");
//	if (mat && mat != _material)
//		{
//			QMessageBox::information(this,
//															 "Material - " CCE_TOOL_NAME,
//															 "The material name is already in use.\nPlease enter a unique material name.");
//			return false;
//		}
//	_material->SetName(_gui.lineEditName->text() + ".mat");


//  cceMaterialMaterialSpecTechniqueItem* spec = _primaryItem->GetSpec();

//  _material->SetAmbient(spec->GetAmbient()->GetColor());
//  _material->SetDiffuse(spec->GetDiffuse()->GetColor());
//  _material->SetSpecular(spec->GetSpecular()->GetColor());
//  _material->SetShininess(spec->GetShininess()->GetShininess());
//  _material->SetEmission(spec->GetEmission()->GetColor());
//  _material->SetWriteDepth(spec->GetWriteDepth()->IsChecked());
//  _material->SetTestDepth(spec->GetTestDepth()->IsChecked());
//  _material->SetTransparent(spec->GetTransparent()->IsChecked());

//  _material->SetEffect(_primaryItem->GetEffect());
//  for (unsigned p=0; p<Pass_COUNT; ++p)
//    {
//      cceMaterialPass pass = (cceMaterialPass)p;
//      for (unsigned u=0; u<16; ++u)
//        {
//          _material->SetTexture(pass, u, _primaryItem->GetTexture(pass, u));
//        }
//    }

//  if (_material->IsNew() && _manager)
//    {
//      _manager->AddMaterial(_material);
//    }

//	SetChanged(false);
//  return true;
//}


